const express = require('express');
const Response = require('../utils/Response.js')
const pool = require('../utils/db.js')
const joi = require('joi')
const router = express.Router();




/**
 * 查询所有门票
 * @param:
 *   page: 1       当前页码
 *   pagesize: 10  每页条目数
 * @returns:
 *   {code:200, msg:'ok', data:[{门票Obj},{门票Obj},{门票Obj}]}
 */
router.get('/ticket/list', function (req, res) {
  let { title, page, pagesize } = req.query;
  let schema = joi.object({
    title: joi.any(),
    page: joi.number().default(1).required(), // page必须是数字，必填
    pagesize: joi.number().default(10).required(), // pagesize必须是不大于100的数字，必填
  });
  let { error, value } = schema.validate(req.query);
  if (error) {
    res.send(Response.error(400, error));
    return;
  }
  // 模糊查询
  let startIndex = (page - 1) * 10;
  let size = parseInt(pagesize);
  if (title == undefined || title == '') {
    let countSql = "select count(*) total from ticket";
    let totalNumber = 0;
    pool.query(countSql, [], (err, countResult) => {
      if (err) {
        res.send(Response.error(500, err));
      } else {
        // 将结果封装，返回给客户端
        totalNumber = countResult[0].total
        let sql = "select * from ticket limit ?,?";
        pool.query(sql, [startIndex, size], (err, result) => {
          if (err) {
            res.send(Response.error(500, err));
          }
          // 将结果封装，返回给客户端
          res.json({
            code: 200,
            msg: "OK",
            data: {
              result: result,
              total: totalNumber,
              page: page,
              pagesize: pagesize
            } // 查询结果
          });
        });
      }
    });
  } else {
    let countSql = "select count(*) total from ticket where title like ?";
    let totalNumber = 0;
    pool.query(countSql, [`%${title}%`], (err, countResult) => {
      if (err) {
        res.send(Response.error(500, err));
      } else {
        // 将结果封装，返回给客户端
        totalNumber = countResult[0].total;
        let sql = "select * from ticket where title like ? limit ?,?";
        pool.query(sql, [`%${title}%`, startIndex, size], (err, result) => {
          if (err) {
            res.send(Response.error(500, err));
          }
          // 将结果封装，返回给客户端
          res.json({
            code: 200,
            msg: "OK",
            data: {
              result: result,
              total: totalNumber,
              page: page,
              pagesize: pagesize
            }
          });
        });
      }
    });
  }
});

/**
 * 查询所有门票
 * @param:
 *   tid: 门票ID
 * @returns:
 *   {code:200, msg:'ok', data:{门票Obj}
 */
router.get('/ticket/detail', function (req, res) {
  let { tid } = req.query;
  let schema = joi.object({
    tid: joi.number().required(), //tid必填
  });
  let { error, value } = schema.validate(req.query);
  if (error) {
    res.send(Response.error(400, error));
    return;
  }
  //根据ID查询
  let sql = "select * from ticket where tid = ?";
  pool.query(sql, [tid], (err, result) => {
    if (err) {
      res.send(Response.error(500, err));
    }
    // 将结果封装，返回给客户端
    res.send(Response.ok(result[0]));
  });
});

module.exports = router;